home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / common / porting.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  6KB  |  252 lines

  1.  
  2. /*
  3.  * static char *rcsid_porting_c =
  4.  *   "$Id: porting.c,v 1.7 1996/01/02 10:57:27 master Exp $";
  5.  */
  6.  
  7. /*
  8.     CrossFire, A Multiplayer game for X-windows
  9.  
  10.     Copyright (C) 1994 Mark Wedel
  11.     Copyright (C) 1992 Frank Tore Johansen
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27.     The author can be reached via e-mail to master@rahul.net
  28. */
  29.  
  30. /* This file contains various functions that are not really unique for
  31.  * crossfire, but rather provides what should be standard functions 
  32.  * for systems that do not have them.  In this way, most of the
  33.  * nasty system dependent stuff is contained here, with the program
  34.  * calling these functions.
  35.  */
  36.  
  37.  
  38.  
  39. #include "global.h"
  40. #include <ctype.h>
  41. #include <sys/stat.h>
  42. /*#include <xdir.h>*/
  43.  
  44. #if defined (MACH) || defined(vax) || defined(ibm032) || defined(NeXT)
  45. #  ifdef NeXT
  46. #    undef BSD
  47. #  endif
  48. #  include <sys/param.h>
  49. #  include <stdio.h>
  50. #  if defined(MACH)
  51. #    include <stdlib.h>
  52. #    include <unistd.h>
  53. #  endif
  54.  
  55. #if defined(MACH) || defined(vax) || defined(ibm032) || defined(NeXT) || defined(__MACH__)
  56. #ifndef S_ISDIR
  57. #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
  58. #endif
  59. #endif
  60.  
  61. static unsigned int curtmp = 0;
  62.  
  63. /*
  64.  * A replacement for the tempnam() function since it's not defined
  65.  * at some unix variants.
  66.  */
  67.  
  68. char *tempnam(char *dir, char *pfx)
  69. {
  70.   char *f, *name;
  71.  
  72.   if (!(name = (char *) malloc(MAXPATHLEN)))
  73.     return(NULL);
  74.  
  75.   if (!pfx)
  76.     pfx = "tmp.";
  77.  
  78.   if (f = (char *) getenv("TMPDIR")) {
  79. #if !defined(MACH) || defined(NeXT)
  80.     (void)sprintf(name,"%s/%s%d", f, pfx, curtmp);
  81. #else
  82.     (void)snprintf(name, MAXPATHLEN, "%s/%s%d", f, pfx, curtmp);
  83. #endif
  84.     curtmp++;
  85.     return(name);
  86.   }
  87.   if (f = (char *)dir) {
  88. #if !defined(MACH) || defined(NeXT)
  89.     (void)sprintf(name,"%s/%s%d", f, pfx, curtmp);
  90. #else
  91.     (void)snprintf(name, MAXPATHLEN, "%s/%s%d", f, pfx, curtmp);
  92. #endif
  93.     curtmp++;
  94.     return(name);
  95.   }
  96.   return(NULL);
  97. }
  98. #endif /* MACH || vax || ibm032 || NeXT */
  99.  
  100.  
  101. /*
  102.  * A replacement of strdup(), since it's not defined at some
  103.  * unix variants.
  104.  */
  105.  
  106. char *strdup_local(char *str) {
  107.   char *c=(char *)malloc(sizeof(char)*(strlen(str)+1));
  108.   strcpy(c,str);
  109.   return c;
  110. }
  111.  
  112.  
  113. #define DIGIT(x)        (isdigit(x) ? (x) - '0' : \
  114. islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
  115. #define MBASE ('z' - 'a' + 1 + 10)
  116.  
  117. /*
  118.  * A replacement of strtol() since it's not defined at
  119.  * many unix systems.
  120.  */
  121.  
  122. long strtol_local(str, ptr, base)
  123.      register char *str;
  124.      char **ptr;
  125.      register int base;
  126. {
  127.   register long val;
  128.   register int c;
  129.   int xx, neg = 0;
  130.  
  131.   if (ptr != (char **) 0)
  132.     *ptr = str;         /* in case no number is formed */
  133.   if (base < 0 || base > MBASE)
  134.     return (0);         /* base is invalid */
  135.   if (!isalnum (c = *str)) {
  136.     while (isspace (c))
  137.       c = *++str;
  138.     switch (c) {
  139.     case '-':
  140.       neg++;
  141.     case '+':
  142.       c = *++str;
  143.     }
  144.   }
  145.   if (base == 0)
  146.     if (c != '0')
  147.       base = 10;
  148.     else
  149.       if (str[1] == 'x' || str[1] == 'X')
  150.         base = 16;
  151.       else
  152.         base = 8;
  153.   /*
  154.    ** For any base > 10, the digits incrementally following
  155.    ** 9 are assumed to be "abc...z" or "ABC...Z"
  156.    */
  157.   if (!isalnum (c) || (xx = DIGIT (c)) >= base)
  158.     return 0;           /* no number formed */
  159.   if (base == 16 && c == '0' && isxdigit (str[2]) &&
  160.       (str[1] == 'x' || str[1] == 'X'))
  161.     c = *(str += 2);    /* skip over leading "0x" or "0X" */
  162.   for (val = -DIGIT (c); isalnum (c = *++str) && (xx = DIGIT (c)) < base;)
  163.     /* accumulate neg avoids surprises near
  164.        MAXLONG */
  165.     val = base * val - xx;
  166.   if (ptr != (char **) 0)
  167.     *ptr = str;
  168.   return (neg ? val : -val);
  169. }
  170. /*
  171.  * 'Small' tool funtions for help
  172.  */
  173.  
  174.  
  175. struct xdirect *xreaddir(dir_ptr, mask)
  176.      XDIR *dir_ptr;
  177.      int mask;
  178. {
  179.   static struct xdirect xde;
  180. #if defined(_AIX) || defined(M_UNIX) && !defined(NeXT)
  181.   struct dirent *de;
  182. #else
  183.   struct direct *de;
  184. #endif
  185.   int namelen;
  186.   struct stat st;
  187.  
  188.   de = readdir(dir_ptr);
  189.   if (!de) return 0;
  190. #ifdef POSIX
  191.   namelen = strlen(de->d_name);
  192. #else
  193.   namelen = de->d_namlen;
  194. #endif
  195.   xde.d_namlen = namelen;
  196.   xde.d_name   = de->d_name;
  197.   if (mask & (2|4) ) {
  198.     if (stat(xde.d_name, &st) == -1) { /* who knows... */
  199.       xde.size = -1;
  200.       xde.time = 0;
  201.     } else {
  202.       if (S_ISDIR(st.st_mode))
  203.         xde.size = -2;
  204.       else
  205.         xde.size = st.st_size;
  206.       xde.time = st.st_mtime;
  207.   }
  208.     }
  209.   return &xde;
  210. }
  211.  
  212. /* This seems to be lacking on some system */
  213. #if defined(SVR4) && !defined(__sgi__) && !defined(sun)
  214. int strncasecmp(char *s1, char *s2, int n)
  215. {
  216.   register int c1, c2;
  217.  
  218.   while (*s1 && *s2 && n) {
  219.     c1 = tolower(*s1);
  220.     c2 = tolower(*s2);
  221.     if (c1 != c2)
  222.       return (c1 - c2);
  223.     s1++;
  224.     s2++;
  225.     n--;
  226.   }
  227.   if (!n)
  228.     return(0);
  229.   return (int) (*s1 - *s2);
  230. }
  231.  
  232.  
  233. #if !defined(Xpm_Pix)
  234. int strcasecmp(char *s1, char*s2)
  235. {
  236.   register int c1, c2;
  237.  
  238.   while (*s1 && *s2) {
  239.     c1 = tolower(*s1);
  240.     c2 = tolower(*s2);
  241.     if (c1 != c2)
  242.       return (c1 - c2);
  243.     s1++;
  244.     s2++;
  245.   }
  246.   if (*s1=='\0' && *s2=='\0')
  247.     return 0;
  248.   return (int) (*s1 - *s2);
  249. }
  250. #endif
  251. #endif
  252.